package com.sicau.basedemo.controller;


import com.sicau.basedemo.common.constant.UrlConstants;
import com.sicau.basedemo.common.returning.ResultData;
import com.sicau.basedemo.config.aop.ValidateAspect;
import com.sicau.basedemo.entity.param.news.NewsAddParam;
import com.sicau.basedemo.entity.param.news.NewsQueryParam;
import com.sicau.basedemo.entity.param.news.NewsUpdateParam;
import com.sicau.basedemo.entity.vo.PageDataVO;
import com.sicau.basedemo.entity.vo.practicenews.PracticeNewsVO;
import com.sicau.basedemo.service.NewsService;
import com.sicau.basedemo.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;

/**
 * <p>
 * 团队新闻稿表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@RequestMapping(UrlConstants.PRACTICE_NEWS_PREFIX)
@Slf4j
@Api(tags = "新闻稿相关")
public class NewsController {

    @Resource
    private NewsService newsService;

    @PostMapping
    @ApiOperation("发新闻稿")
    @ValidateAspect.ParamValidate
    public ResultData postNews(@RequestBody @Valid NewsAddParam newsAddParam, BindingResult bindingResult) {
        newsService.add(newsAddParam);
        return new ResultData().OK();
    }

    @PostMapping("/file")
    @ApiOperation("上传文件(form-data形式传参)")
    public ResultData<String> postNews(@RequestParam MultipartFile file){
        String fullPath = newsService.uploadFile(file);
        return new ResultData<String>().OK().setObj(fullPath);
    }

    @GetMapping("/file")
    @ApiOperation("获取文件")
    public void getFile(String fullPath, HttpServletResponse response) {
        response.setContentType("application/x-download;charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + fullPath.substring(fullPath.lastIndexOf(File.separator)+1));
        File file = new File(fullPath);
        try {
            InputStream stream = new FileInputStream(file);
            ServletOutputStream out = response.getOutputStream();
            byte buff[] = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) > 0) {
                out.write(buff,0,length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PutMapping
    @ApiOperation("改新闻稿")
    @ValidateAspect.ParamValidate
    public ResultData updateNews(@RequestBody @Valid NewsUpdateParam newsUpdateParam, BindingResult bindingResult) {
        newsService.update(newsUpdateParam);
        return new ResultData().OK();
    }

    @GetMapping("/page")
    @ApiOperation("查询新闻稿，条件可以为空（查全部）")
    @ValidateAspect.ParamValidate
    public ResultData<PageDataVO<PracticeNewsVO>> getPage(@Valid NewsQueryParam newsQueryParam, BindingResult bindingResult) {
        return new ResultData<PageDataVO<PracticeNewsVO>>().OK().setObj(PageUtil.convert(newsService.queryPage(newsQueryParam)));
    }

    @GetMapping
    @ApiOperation("根据id查询")
    public ResultData<PracticeNewsVO> getById(Integer id) {
        return new ResultData<PracticeNewsVO>().OK().setObj(newsService.queryVOById(id));
    }


}

